#include "BinaryTree.h"


BinaryTree::BinaryTree()
{
    root = nullptr;
}

BinaryTree::~BinaryTree()
{
    Free(root);
}

void BinaryTree::Insert(const ElementType &element)
{
    root = InsertNode(root,element);
}

void BinaryTree::Remove(const ElementType &element)
{
    root = RemoveNode(root,element);
}

BinaryTree::ElementType *BinaryTree::Find(const ElementType &element)
{
    return FindNode(root,element);
}

BinaryTree::TreeNode* BinaryTree::InsertNode(TreeNode *node, const ElementType &element)
{
    if(node == nullptr)
    {
        return new TreeNode(element);
    }

    if(node->data == element)
    {
        return node;
    }
    else if(node->data > element)
    {
        node->left = InsertNode(node->left,element);
        //左边重
        if(LRDevition(node) > 1)
        {
            //左左
            if(node->left->data > element)
            {
                //右旋
                node = RotateRight(node);
            }
            else
            //左右失衡
            {
                //左右旋
                node = RotateLeftRight(node);
            }
        }
    }
    else
    {
        node->right = InsertNode(node->right,element);
        //右边重
        if(LRDevition(node) < -1)
        {
             //右右
            if(node->right->data < element)
            {
               node = RotateLeft(node);

            }
            else
            //右左
            {
                node = RotateRightLeft(node);
            }
        }
    }
    return node;
    
}

void BinaryTree::show()
{
    std::cout<<"前序遍历:";
    PrevTravel(root);
    std::cout<<std::endl;

    std::cout<<"中序遍历:";
    MidTravel(root);
    std::cout<<std::endl;
    
    std::cout<<"后序遍历:";
    PostTravel(root);
    std::cout<<std::endl;

    std::cout<<"层序遍历:";
    LinkQueue q;
    q.Push(root);
    while(!q.IsEmpty())
    {
        TreeNode *node = (TreeNode*)*q.GetFront();
        q.Pop();
        std::cout<<node->data<<" ";
        if(node->left)
            q.Push(node->left);
        if(node->right)
            q.Push(node->right);
    }
    std::cout<<std::endl;
}

void BinaryTree::PrevTravel(TreeNode *node)
{
    if(node == nullptr)
        return;
    std::cout<<node->data<<" ";
    PrevTravel(node->left);
    PrevTravel(node->right);
}

void BinaryTree::MidTravel(TreeNode *node)
{
     if(node == nullptr)
        return;
    
    MidTravel(node->left);
    std::cout<<node->data<<" ";
    MidTravel(node->right);
}

void BinaryTree::PostTravel(TreeNode *node)
{
     if(node == nullptr)
        return;
    PostTravel(node->left);
    PostTravel(node->right);
    std::cout<<node->data<<" ";
}

void BinaryTree::Free(TreeNode *node)
{
    if(node == nullptr)
        return; 
    Free(node->left);
    Free(node->right);
    delete node;
}

BinaryTree::TreeNode* BinaryTree::RemoveNode(TreeNode *node, const ElementType &element)
{
    if(node ==  nullptr)
        return nullptr;
    if(node->data < element)
    {
        node->right = RemoveNode(node->right,element);
    }
    else if(node->data > element)
    {
        node->left = RemoveNode(node->left,element);
    }
    else
    {
        //叶子
        if(node->left == nullptr && node->right == nullptr)
        {
            delete node;
            return nullptr;
        }
        else if(node->left && node->right )
        {
            //取右子树中最小结点
            TreeNode* minNode = GetMinNode(node->right);
            node->data = minNode->data;
            node->right = RemoveNode(node->right,minNode->data);
        }
        //有一个孩子
        else
        {
            TreeNode* child = node->left ? node->left : node->right;
            delete node;
            return child;
        }

    }
    //左边重
    if(LRDevition(node) > 1)
    {
        //左左
        if(LRDevition(node->left) > 0)
        {
            //右旋
            node = RotateRight(node);
        }
        else
        //左右失衡
        {
            //左右旋
            node = RotateLeftRight(node);
        }
    }
    if(LRDevition(node) < -1)
    {
            //右右
        if(LRDevition(node->right) < 0)
        {
            node = RotateLeft(node);

        }
        else
        //右左
        {
            node = RotateRightLeft(node);
        }
    }
    return node;
}

BinaryTree::TreeNode * BinaryTree::GetMinNode(TreeNode *node)
{
    TreeNode *minNode = node;
    while(minNode->left)
        minNode = minNode->left;
    return minNode;
}

BinaryTree::ElementType *BinaryTree::FindNode(TreeNode *node,const ElementType &element)
{
    if(node == nullptr)
        return nullptr;
    if(node->data == element)
        return &node->data;
    else if(node->data > element)
        return FindNode(node->left,element);
    else
        return FindNode(node->right,element);
}

BinaryTree::TreeNode *BinaryTree::RotateRight(TreeNode *root)
{
    TreeNode *t = root->left;
    root->left = t->right;
    t->right = root;
    return t;
}

BinaryTree::TreeNode *BinaryTree::RotateLeft(TreeNode *root)
{
    TreeNode *t = root->right;
    root->right = t->left;
    t->left = root;
    return t;
}

BinaryTree::TreeNode *BinaryTree::RotateLeftRight(TreeNode *root)
{
    //左子树左旋
    root->left = RotateLeft(root->left);
    return RotateRight(root);//整棵树右旋
}

BinaryTree::TreeNode *BinaryTree::RotateRightLeft(TreeNode *root)
{
    //右子树右旋
    root->right = RotateLeft(root->right);
    return RotateLeft(root);//整棵树左旋
} 

int BinaryTree::GetNodeHight(TreeNode *node)
{
    if(node == nullptr)
        return 0;
    int leftHight = GetNodeHight(node->left);
    int rightHight = GetNodeHight(node->right);
    return (leftHight > rightHight ? leftHight : rightHight) + 1;
}

int BinaryTree::LRDevition(TreeNode *node)
{
    if(node == nullptr)
        return 0;
    return GetNodeHight(node->left) - GetNodeHight(node->right);
}
